//--- Projeto Domus
// interactionsource.com
// Koji Pereira, Andre Veloso e Marcos Paulo

/*
 Projeto Domus
 interactionsource.com
 Koji Pereira, Andre Veloso e Marcos Paulo

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){

    vidGrabber.setVerbose(true);
    vidGrabber.initGrabber(320,240);

    colorImg.allocate(320,240);
	grayImage.allocate(320,240);
	grayBg.allocate(320,240);
	grayDiff.allocate(320,240);

	bLearnBakground = true;
	threshold = 80;

	isCena01 = false;
	ofSetBackgroundAuto(false);
        ofBackground (0,0,0);

}


//--------------------------------------------------------------
void testApp::update(){
//	ofBackground(100,100,100);
    if (isCena01) {
		ofSetColor(0,0,0);	    // black, X% transparent
		ofRect(700,0,740,600);
		isCena01=false;
		}

        ofDisableAlphaBlending();
//                alphax = 1;
//            }
//    }

        ofFill();
        ofSetColor(0x000000);
		ofRect(0,0,700,600);

    bool bNewFrame = false;

        vidGrabber.grabFrame();
        bNewFrame = vidGrabber.isFrameNew();

	if (bNewFrame){
        colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);

        grayImage = colorImg;
		if (bLearnBakground == true){
			grayBg = grayImage;		// the = sign copys the pixels from grayImage into grayBg (operator overloading)
			bLearnBakground = false;
		}

		// take the abs value of the difference between background and incoming and then threshold:
		grayDiff.absDiff(grayBg, grayImage);
		grayDiff.threshold(threshold);


		// find contours which are between the size of 20 pixels and 1/3 the w*h pixels.
		// also, find holes is set to true so we will get interior contours as well....
		contourFinder.findContours(grayDiff, 20, 5000000, 1, true);	// find holes
    // usage: findContours( ofxCvGrayscaleImage& input, int minArea, int maxArea, int nConsidered, bool bFindHoles, bool bUseApproximation = true);

	}

}

//--------------------------------------------------------------
void testApp::draw(){


	// draw the incoming, the grayscale, the bg and the thresholded difference, just putting in right position
	ofSetColor(0xffffff);
	colorImg.draw(20,20);
	grayImage.draw(360,20);
	grayBg.draw(20,280);
	grayDiff.draw(360,280);

	// or, instead we can draw each blob individually,
	// this is how to get access to them:
    //    for (int i = 0; i < contourFinder.nBlobs; i++){
    //    contourFinder.blobs[i].draw(700,20);
    //    }

    //-- Lines connecting
	for (int i = 0; i < contourFinder.nBlobs; i++){
    ofSetColor(0xffffff);
    ofFill;
    ofSetLineWidth(5);
    // -- Creating way light
    for( int j=0; j<100; j++ ) {
        ofCircle(contourFinder.blobs[i].centroid.x*2+700, contourFinder.blobs[i].centroid.y*2+30, contourFinder.blobs[i].boundingRect.height-contourFinder.blobs[i].boundingRect.width+j*-1 );
        ofEnableAlphaBlending();	// turn on alpha blending
		ofSetColor(0, 255,255,5);	    // black, X% transparent
        ofCircle(contourFinder.blobs[i].centroid.x*2+700, contourFinder.blobs[i].centroid.y*2+30, contourFinder.blobs[i].boundingRect.height-contourFinder.blobs[i].boundingRect.width+j+20*-1 );
        ofCircle(contourFinder.blobs[i].centroid.x*2+700, contourFinder.blobs[i].centroid.y*2+30, contourFinder.blobs[i].boundingRect.height-contourFinder.blobs[i].boundingRect.width+j+50*-1 );

    }
    }



	// finally, a report:
	ofSetColor(0xffffff);
	char reportStr[1024];
	sprintf(reportStr, "bg subtraction and blob detection\naperte'espaco' para apagar o bg\nthreshold %i (press: +/-)\naperte 1 para abrir a cena\nnum blobs %i, fps: %f", threshold, contourFinder.nBlobs, ofGetFrameRate());
	ofDrawBitmapString(reportStr, 20, 540);

}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){

	switch (key){
		case ' ':
			bLearnBakground = true;
			break;
		case '+':
			threshold ++;
			if (threshold > 255) threshold = 255;
			break;
		case '-':
			threshold --;
			if (threshold < 0) threshold = 0;
			break;
		case '1':
            ofSetFrameRate(60);
			isCena01 = true;
			break;
	}
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

